package com.ygq.manageGame.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ygq.manageGame.bean.Game;
import com.ygq.manageGame.service.GameService;
import com.ygq.manageGame.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Slf4j
@RestController
public class GameController {

    @Resource
    private GameService gameService;

    @PostMapping("/save")
    public Result SaveGame(@Validated @RequestBody Game game, Errors errors) {
        Map<String, Object> map = new HashMap<>();
        List<FieldError> fieldErrors = errors.getFieldErrors();
        for (FieldError e : fieldErrors) {
            map.put(e.getField(), e.getDefaultMessage());
        }
        if (map.isEmpty()) {
            gameService.save(game);
            return Result.success();
        } else {
            return Result.error(400, "校验失败", map);
        }
    }


//    @GetMapping("/list")
//    public List<Game> getAllGame(){
//        List<Game> list = gameService.list();
//        for (Game game : list) {
//            System.out.println(game);
//        }
//        return list;
//    }

    @GetMapping("/games")
    public Result showGame() {

        List<Game> lists = gameService.list();
        return Result.success(lists);
    }

    /**
     * @param game
     * @return
     * @RequestBody 表示数据是从前端/浏览器 以json格式发过来的
     */
    @PutMapping("/update")
    public Result UpdateGame(@RequestBody Game game) {
        gameService.updateById(game);
        return Result.success(game);
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Result<?> del(@PathVariable Integer id) {
        gameService.removeById(id);
        return Result.success();
    }

    @GetMapping("/find/{id}")
    public Result findGame(@PathVariable Integer id) {

        Game byId = gameService.getById(id);
        System.out.println(byId);
        return Result.success(byId);
    }


    @ResponseBody
    @RequestMapping("/gamesByPage")
    public Result listGamesByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "5") Integer pageSize) {

        Page<Game> gamePages = gameService.page(new Page<>(pageNum, pageSize));
        return Result.success(gamePages);
    }

    @RequestMapping("/gameSearchPage")
    public Result GameBySearcherByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "5") Integer pageSize,
                                       @RequestParam(defaultValue = "") String search) {

        QueryWrapper<Game> query = Wrappers.query();
        if (StringUtils.hasText(search)) {
            query.like("name", search);
        }
        Page<Game> gamePage = gameService.page(new Page<>(pageNum, pageSize), query);
        return Result.success(gamePage);

    }

    @RequestMapping("/gameSearchPage2")
    public Result GameBySearcherByPage2(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "5") Integer pageSize,
                                       @RequestParam(defaultValue = "") String search) {

        LambdaQueryWrapper<Game> queryWrapper=new LambdaQueryWrapper<>();

        //这里使用了lombda表达式
        SFunction<Game, Object> fs = Game::getName;
        queryWrapper.like(fs,search);
       Page<Game> page= gameService.page(new Page<>(pageNum,pageSize),queryWrapper);
       log.info("page={}",page.getRecords());
       return Result.success(page);

    }

}
